package morning.cat;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import morning.cat.service.UserService;
import morning.cat.service.impl.UserServiceImpl;

import java.io.IOException;

/**
 * Service Bootstrap
 */
public class DubboApiProviderBootstrap {

    private static Logger logger = LoggerFactory.getLogger(DubboApiProviderBootstrap.class);

    /**
     * 启动入口
     */
    public static void main(String[] args) {

        // 服务实现
        UserService userService = new UserServiceImpl();

        // 当前应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("sample-dubbo-provider-spring");

        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        //registry.setUsername("aaa");
        //registry.setPassword("bbb");

        // 服务提供者协议配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(8087);
        protocol.setThreads(200);

        // 注意：ServiceConfig为重对象，内部封装了与注册中心的连接，以及开启服务端口

        // 服务提供者暴露服务配置
        // 此实例很重，封装了与注册中心的连接，请自行缓存，否则可能造成内存和连接泄漏
        ServiceConfig<UserService> service = new ServiceConfig<UserService>();
        service.setApplication(application);
        service.setRegistry(registry); // 多个注册中心可以用setRegistries()
        service.setProtocol(protocol); // 多个协议可以用setProtocols()
        service.setInterface(UserService.class);
        service.setRef(userService);
        service.setVersion("1.0");

        // 暴露及注册服务
        service.export();

        try {
            // press any key to exit
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
